home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / ComeBackHere / source code / slowsizer / main.c next >
Encoding:
C/C++ Source or Header  |  1997-06-28  |  2.8 KB  |  103 lines  |  [TEXT/CWIE]

  1. // main.c
  2.  
  3. #include <A4Stuff.h>
  4. #include <Resources.h>
  5. #include <Windows.h>
  6.  
  7. #define OnscreenWindow(window) (IsWindowVisible(window) && !EmptyRgn(((WindowPtr)window)->visRgn))
  8.  
  9. typedef pascal void (*sizeWindow)(WindowRef theWindow, short w, short h, Boolean fUpdate);
  10. sizeWindow gSizeWindowAddr;
  11. pascal void MySizeWindow(WindowRef theWindow, short w, short h, Boolean fUpdate);
  12.  
  13. typedef pascal void (*hideWindow)(WindowRef theWindow);
  14. hideWindow gHideWindowAddr;
  15. pascal void MyHideWindow(WindowRef theWindow);
  16.  
  17. typedef pascal void (*showHide)(WindowRef theWindow, Boolean showFlag);
  18. showHide gShowHideAddr;
  19. pascal void MyShowHide(WindowRef theWindow, Boolean showFlag);
  20.  
  21. void SlowCloseWindow(WindowPeek window);
  22.  
  23. void main(void) {
  24.     Handle init;
  25.  
  26.     EnterCodeResource();
  27.     
  28.     init = Get1Resource('INIT', 0);
  29.     DetachResource(init);
  30.  
  31.     gSizeWindowAddr = (sizeWindow)NGetTrapAddress(_SizeWindow, ToolTrap);
  32.     NSetTrapAddress((UniversalProcPtr)MySizeWindow, _SizeWindow, ToolTrap);
  33.  
  34.     gHideWindowAddr = (hideWindow)NGetTrapAddress(_HideWindow, ToolTrap);
  35.     NSetTrapAddress((UniversalProcPtr)MyHideWindow, _HideWindow, ToolTrap);
  36.  
  37.     gShowHideAddr = (showHide)NGetTrapAddress(_ShowHide, ToolTrap);
  38.     NSetTrapAddress((UniversalProcPtr)MyShowHide, _ShowHide, ToolTrap);
  39.         
  40.     ExitCodeResource();
  41. }
  42.  
  43. void SlowCloseWindow(WindowPeek window) {
  44.     if (OnscreenWindow(window)) {
  45.         WindowPeek i;
  46.         for (i = (WindowPeek)FrontWindow(); i != nil; i = i->nextWindow) {
  47.             if (i == window) {
  48.                 SizeWindow((WindowPtr)window, 0, 0, false);
  49.                 return;
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55. pascal void MySizeWindow(WindowRef theWindow, short w, short h, Boolean fUpdate) {
  56.     WindowPtr window;
  57.     short oldWidth, oldHeight, currentWidth, currentHeight, i;
  58.     Str255 s;
  59.     
  60.     EnterCodeResource();
  61.  
  62.     window = (WindowPtr)theWindow;
  63.     if (OnscreenWindow(theWindow)) {
  64.         oldWidth = window->portRect.right - window->portRect.left;
  65.         oldHeight = window->portRect.bottom - window->portRect.top;
  66.         
  67.         if (oldHeight != h) {
  68.             short heightSign = h > oldHeight ? 1 : -1;
  69.             currentHeight = oldHeight+heightSign;
  70.             for (i = currentHeight; i != h; i += heightSign) {
  71.                 gSizeWindowAddr(theWindow, oldWidth, (currentHeight += heightSign), false);
  72.             }
  73.         }
  74.         if (oldWidth != w) {
  75.             short widthSign = w > oldWidth ? 1 : -1;
  76.             currentWidth = oldWidth+(2*widthSign);
  77.             for (i = currentWidth; i != w; i += widthSign) {
  78.                 gSizeWindowAddr(theWindow, (currentWidth += widthSign)-1, h, false);
  79.             }
  80.         }
  81.     }
  82.     gSizeWindowAddr(theWindow, w, h, fUpdate);
  83.     
  84.     ExitCodeResource();
  85. }
  86.  
  87. pascal void MyHideWindow(WindowRef theWindow) {
  88.     EnterCodeResource();
  89.     
  90.     SlowCloseWindow((WindowPeek)theWindow);    
  91.     gHideWindowAddr(theWindow);
  92.     
  93.     ExitCodeResource();
  94. }
  95.  
  96. pascal void MyShowHide(WindowRef theWindow, Boolean showFlag) {
  97.     EnterCodeResource();
  98.         
  99.     if (!showFlag) SlowCloseWindow((WindowPeek)theWindow);
  100.     gShowHideAddr(theWindow, showFlag);
  101.     
  102.     ExitCodeResource();
  103. }